package servlet.AdminLogin;

import dao.UserDao;
import domain.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

public class AdminLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;
    UserDao us=new UserDao();
    public AdminLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.getRequestDispatcher("/admin/error/error404.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String rVCode = request.getParameter("vCode");
        String un = request.getParameter("username");
        String pw = request.getParameter("password");
        if (un == null || pw == null) {
            request.getRequestDispatcher("/admin/error/error404.jsp").forward(request,response);
        } else {
            String targetUrl = null;
            String msg = null;
            String erro = null;
            HttpSession session = request.getSession();
            String sVCode = (String) session.getAttribute("vCode");
            session.removeAttribute("vCode");
            if (rVCode != null && sVCode != null && rVCode.equals(sVCode)) {
                if (un != null && pw != null) {
                    User user = null;
                    if ((user = us.verify(un, pw)) != null) {
                        String lastUrl = (String) session.getAttribute("lastUrl");
                        request.getSession().setAttribute("username", un);
                        session = request.getSession(true);
                        session.setAttribute("adminloginflag", user);
                        if (lastUrl != null) {
                            targetUrl = lastUrl;
                        } else {
                            if (user.getUsername().equals("admin"))
                                targetUrl = request.getContextPath() + "/UserInfo";
                            else if (user.getCommunity_power() == 1)
                                targetUrl = request.getContextPath() + "/CommunityInfo";
                            else if (user.getRoom_power() == 1)
                                targetUrl = request.getContextPath() + "/RoomInfo";
                            else if (user.getView_power() != null)
                                targetUrl = request.getContextPath() + "/Info";
                            else
                                targetUrl = request.getContextPath() + "/admin/error/nopower.jsp";
                        }
                    } else {
                        System.out.println("程序执行至此处，无对应用户名密码");
                        msg = "账号密码有误";
                        erro = "yes";
                    }
                } else {
                    System.out.println("输入的账号密码为空");
                    msg = "账号密码有误";
                    erro = "yes";
                }
            } else {
                msg = "验证码有误";
                erro = "yes";
            }
            if (msg != null) {
                targetUrl = request.getContextPath() + "/admin/login.jsp?msg=" + URLEncoder.encode(msg, "utf-8") + "&erro='yes'";
            }
            response.sendRedirect(targetUrl);
        }
    }
}
